<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;
/**
 * 后台默认控制器
 * @author jry <598821125@qq.com>
 */
class JihuomaController extends AdminController
{

    public function index()
    {
        $x = $_GET['select'];
        //获取所有配置
        switch ($x)
        {
            case 1:
                $map['jihuo_zhuangtai'] = array('NEQ', '9'); //禁用和正常状态
                $data_list = D('Jihuoma')->page(!empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map)->select();
                $page = new \Common\Util\Page(D('Jihuoma')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
                $ab=D('Jihuoma')->where($map)->select();
                $user_total=count($ab);

                $mapc['jihuo_zhuangtai'] = array('EQ', '1'); //已激活
                $abc=D('Jihuoma')->where($mapc)->select();
                $user_totalc=count($abc);

                $mapb['jihuo_zhuangtai'] = array('EQ', '0'); //未激活
                $abb=D('Jihuoma')->where($mapb)->select();
                $user_totalb=count($abb);

                $listName = '激活码列表';
                break;
            case 2:
                $map['jihuo_zhuangtai'] = array('EQ', '1'); //禁用和正常状态
                $data_list = D('Jihuoma')->page(!empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map)->select();
                $page = new \Common\Util\Page(D('Jihuoma')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
                $ab=D('Jihuoma')->where($map)->select();
                $user_total=count($ab);
                
                $listName = '已使用';
                break;
            case 3:
                $map['jihuo_zhuangtai'] = array('EQ', '0'); //禁用和正常状态
                $data_list = D('Jihuoma')->page(!empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map)->select();
                $page = new \Common\Util\Page(D('Jihuoma')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
                $ab=D('Jihuoma')->where($map)->select();
                $user_total=count($ab);

                $listName = '未使用';
                break;
            case 4:
                $map['yy_username'] = array('NEQ', '');
                $map['ord_username'] = array('NEQ', '');
                $data_list = D('Jihuoma')->page(!empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map)->select();
                $page = new \Common\Util\Page(D('Jihuoma')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
                $ab=D('Jihuoma')->where($map)->select();
                $user_total=count($ab);

                $mapc['yy_username'] = array('NEQ', '');
                $mapc['ord_username'] = array('NEQ', '');
                $mapc['jihuo_zhuangtai'] = array('EQ', '1'); //已激活
                $abc=D('Jihuoma')->where($mapc)->select();
                $user_totalc=count($abc);

                $mapb['yy_username'] = array('NEQ', '');
                $mapb['ord_username'] = array('NEQ', '');
                $mapb['jihuo_zhuangtai'] = array('EQ', '0'); //未激活
                $abb=D('Jihuoma')->where($mapb)->select();
                $user_totalb=count($abb);

                $listName = '未使用';
                break;
            default:
                $map['jihuo_zhuangtai'] = array('NEQ', '9'); //禁用和正常状态
                $data_list = D('Jihuoma')->page(!empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map)->select();
                $page = new \Common\Util\Page(D('Jihuoma')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
                $ab=D('Jihuoma')->where($map)->select();
                $user_total=count($ab);

                $mapc['jihuo_zhuangtai'] = array('EQ', '1'); //已激活
                $abc=D('Jihuoma')->where($mapc)->select();
                $user_totalc=count($abc);

                $mapb['jihuo_zhuangtai'] = array('EQ', '0'); //未激活
                $abb=D('Jihuoma')->where($mapb)->select();
                $user_totalb=count($abb);

                $listName = '激活码列表';
                break;
        }


        //使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('激活码列表');//设置页面标题
        $this->assign('page',$page->show());
        $this->assign('listname',$listName);
        $this->assign('data_list',$data_list);
        $this->assign('moneytotal',$user_totalc);
        $this->assign('moneyypp',$user_totalb);
        $this->assign('total',$user_total);
        $this->display();
    }
    public function add()
    {

           $iii=I('post.total');
            for($i=0;$i<$iii;$i++){
                $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
                $key = "JH" . $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
                $data['jihuoma']=$key;
                $data['jihuo_zhuangtai']=0;
                $data['creattime']=date("Y-m-d H:i:s");
                $d=D('Jihuoma')->add($data);
            }
            $this->success('激活码生成成功', U('index'));
            



    }
    public function piliang(){
        $shuliang = trim(I('post.piliang'));
        $where['jihuo_zhuangtai']=array('EQ', '0');
        $ab=D('Jihuoma')->where($where)->limit($shuliang)->select();
        $total=count($ab);
        $this->assign('data_list',$ab);
        $this->assign('total',$total);
        $this->display();
    }
//分发激活码
    public function jihuoff(){
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('发放激活码');//设置页面标题
        $this->display();
    }
    //处理发放的激活码
    public function chulifafang(){
        $username_vip = trim(I('post.username')); //发放人用户名
        $shuliang = trim(I('post.total'));
        $User_total=abs($shuliang); //获得发放数量

        $data['username']=array('eq','');
        $data['yy_username']=array('eq','');
        $data['ord_username']=array('eq','');
        $data_up['yy_username']=$username_vip;
        $jihuoma=D('Jihuoma')->where($data)->limit($User_total)->save($data_up);
        $this->success('激活码发放成功'.$jihuoma, U('index'));
    }

    public function addcreat(){
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('生成激活码');//设置页面标题
        $this->display();
    }
    
}